package com.lienlq.voicemusic.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.R.string;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lienlq.voicemusic.R;
import com.lienlq.voicemusic.entity.Artist;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethod;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethodCallback;

public class ArtistFragment extends Fragment {
	
		  private ListView listview = null;
		  private ArrayList<Artist> artistList = new ArrayList<Artist>(); 
		
		  JSONObject jsObject;
		  Object result;
		  JSONArray jsArray;
		  Context context;
			
		  ArtistAdapter adapter=null;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState); 
	     
	        XMLRPCMethod method=new XMLRPCMethod("selectAllArtist", new XMLRPCMethodCallback() {
				
				@Override
				public void CallFinished(Object result) {
					// TODO Auto-generated method stub
					try{
					jsObject=new JSONObject(result.toString());
					jsArray=jsObject.getJSONArray("artist");
					for(int i=0;i<jsArray.length();i++)
						{
							JSONObject obj=jsArray.getJSONObject(i);
							Artist artist=new Artist(obj.getInt("id"),obj.getString("name"), obj.getString("birthday"),obj.getString("img"));
							artistList.add(artist);
						}
					adapter=new ArtistAdapter();
					listview.setAdapter(adapter);	
					}catch(Exception e)
					{
				
					}
					}
				});
	        	Object[] params=null;
	        	method.call(params);
	        	
	        	//ArtistAdapter adapter=new ArtistAdapter(getActivity(), R.layout.item_artist_fragment_artist, artistList);
	    }
	    
	   
	    

	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
	    	View v = inflater.inflate(R.layout.fragment_artist, container, false);
	        listview = (ListView)v.findViewById(R.id.artists_list_fragment_artist);
	        context=getActivity();
	        return v;
	    }

	    @Override
	    public void onSaveInstanceState(Bundle outState) {
	        super.onSaveInstanceState(outState);
	         
	    }
	 
	    
	    
	    
	    class ArtistAdapter extends ArrayAdapter<Artist>{

			
	    	public ArtistAdapter() {
				// TODO Auto-generated constructor stub
	    		super(context,R.layout.item_artist_fragment_artist,artistList);
			}

			@Override
			public View getView(final int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				View row=convertView;
				ViewHolder holder=null;
				if(row==null)
				{
					LayoutInflater inflater=getActivity().getLayoutInflater();
					row=inflater.inflate(R.layout.item_artist_fragment_artist,parent,false);
					holder=new ViewHolder(row);
					row.setTag(holder);
				}
				else
				{
					holder=(ViewHolder)row.getTag();
				}
				holder.populateFrom(artistList.get(position));
				row.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						int artistID=artistList.get(position).getArtistid();
						String artistName=artistList.get(position).getName();
						Intent intent=new Intent(getActivity(),SongsActivity.class);
						Bundle myBundle=new Bundle();
						myBundle.putString("artistname", artistName);
						myBundle.putInt("artistid", artistID);
						intent.putExtras(myBundle);
						startActivity(intent);
						
					}
				});
				return row;
				
			}

			
			
			
	    	
	    }//end class ArtistAdapter
	   
	    
	    
	    static class ViewHolder{
	    	private WebView webview_img;
	    	private TextView txt_name;
	    	private TextView txt_birthday;
	    	
	    	public ViewHolder(View row)
	    	{
	    		webview_img=(WebView)row.findViewById(R.id.webview_imgArtist);
	    		webview_img.setInitialScale(50);
	    		webview_img.getSettings().setUseWideViewPort(true);
	    		txt_name=(TextView)row.findViewById(R.id.artistname);
	    		txt_birthday=(TextView)row.findViewById(R.id.artistbirthday);
	    	}
	    	void populateFrom(Artist artist)
	    	{
	    		webview_img.loadUrl(artist.getImage());
	    		txt_name.setText(artist.getName());
	    		txt_birthday.setText(artist.getBirthday());
	    		
	    	}
	    }
}
